<script setup>
import api from '../api/Classification_api.js'
import {computed, ref, onMounted} from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import lodash from "lodash";

const tableData = ref({
  data: []
});
const editWinVisible = ref(false);
const addWinVisible = ref(false);
const total=ref(50);

//查询数据
const serchObj = ref({
  current: 1,
  limit: 10,
  total: 0,
  typeName: '',
  typeDes: ''
})

//要修改或者新增的数据
const obj = ref({});
//要查询的数据
const search = ref('');
//要添加的数据
const addForm = ref('');

onMounted(async () => {
  sear();
});
const sear = async()=>{
  const res = await api.page(serchObj.value);
  tableData.value.data = res.data;
  tableData.value.count = res.count;
  total.value=res.count;
  console.log("切换到页码:");
}

const filteredData = computed(() =>
    tableData.value.data.filter(
        (item) =>
            !search.value ||
            (item.name && item.name.toLowerCase().includes(search.value.toLowerCase()))
    )
)
const handlerSearch=()=>{
  serchObj.value.current=1;
  sear();
}
const handleAdd=()=>{
  addForm.value = {};
  api.list();
  addWinVisible.value=true;
}
const addConcel=()=>{
  addForm.value={};
  addWinVisible.value=false;
}
const addCommit=async()=>{
  await api.add(addForm.value);
  sear();
  addWinVisible.value=false;
  ElMessage.success('添加成功');
}
const handlePageChange = (newPage) => {
  serchObj.value.current = newPage; // 确保查询参数更新
  sear(); // 重新加载数据
};
const handleEdit = (index, row) => {
  obj.value=lodash.clone(row);
  editWinVisible.value=true;
  console.log(index, row)
}
const editConcel =()=>{
  obj.value={};
  editWinVisible.value=false;
}
const editCommit=async()=>{
  await api.edit(obj.value);
  sear();
  editWinVisible.value=false;
}
// const handleDelete = (index, row) => {
//   ElMessageBox.confirm(
//       '是否确定删除该类型',
//       '注意！',
//       {
//         confirmButtonText: '确定',
//         cancelButtonText: '取消',
//         type: 'warning',
//       }
//   )
//       .then(async () => {
//         await api.del(row.id);
//         //设置从第一页开始查
//         serchObj.value.current=1;
//         sear();
//
//       })
//       .catch(() => {
//
//       })
// }
</script>

<template>
  <el-form :inline="true" :model="serchObj" class="demo-form-inline" style="margin-top: 10px">
    <el-form-item style="border-bottom-left-radius: 10px" label="类型名称">
      <el-input v-model="serchObj.typeName" placeholder="请输入类型名称" clearable/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handlerSearch">查询</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="filteredData" style="width: 100%">
    <el-table-column label="类型名称" prop="typeName"/>
    <el-table-column label="类型介绍" prop="typeDes"/>
    <el-table-column align="right">
      <template #header>
        <el-button type="primary" @click="handleAdd()">添加</el-button>
      </template>
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
          修改
        </el-button>
<!--        <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">-->
<!--          删除-->
<!--        </el-button>-->
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
      v-model:current-page="serchObj.current"
      :page-size="serchObj.limit"
      :total="total"
      :hide-on-single-page="true"
      layout="prev, pager, next"
      @current-change="handlePageChange"
  />
  <el-dialog v-model="editWinVisible" title="编辑" width="500">
    <el-form :model="obj">
      <el-form-item label="类型名称" :label-width="100">
        <el-input v-model="obj.typeName" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="类型描述" :label-width="100">
        <el-input v-model="obj.typeDes" autocomplete="off"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="editConcel">取消</el-button>
        <el-button @click="editCommit">提交</el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog v-model="addWinVisible" title="编辑" width="500">
    <el-form :model="obj">
      <el-form-item label="类型名称" :label-width="100">
        <el-input v-model="addForm.typeName" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="类型描述" :label-width="100">
        <el-input v-model="addForm.typeDes" autocomplete="off"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="addConcel">取消</el-button>
        <el-button type="primary" @click="addCommit">提交</el-button>
      </div>
    </template>
  </el-dialog>
</template>
